
$(document).ready(function() {

    $("form").submit(function () {
        return false;
    });
    
    var VAL = {
        NORMAL: "0000000",
        EVT_USER: $("#userId").text(),
        T_FBPBISDAT: "FBPBISDAT",
        T_FBPBISTOL: "FBPBISTOL",
        T_MBPREPMAG: "MBPREPMAG",
    };
    
    var domObj = {
        $SetupInfoListDiv: $("#SetupInfoListDiv"),
        $SetupInfoListGrd: $("#SetupInfoListGrd"),
        $ToolIDSel: $("#ToolIDSel"),
        $RecipeIdTxt: $("#RecipeIdTxt"),
        $BodyNameSel: $("#BodyNameSel"),
        $BodyValueMinTxt: $("#BodyValueMinTxt"),
        $BodyValueMaxTxt: $("#BodyValueMaxTxt"),
        $BodyNoteTxt: $("#BodyNoteTxt"),

        buttons: {
            $query_btn: $("#query_btn"),
            $add_btn: $("#add_btn"),
            $save_btn: $("#save_btn"),
            $delete_btn: $("#delete_btn")
        }
    };
    
    var iniGridInfo = function () {
        var SetupInfoCM = [
            {name: 'tool_id_fk',     index: 'tool_id_fk',  label: "设备代码", width: '200'},
            {name: 'body_seq_id',    index: 'body_seq_id', label: "项目序列号", width: '200',hidden:true},
            {name: 'recipe_body_id', index: 'recipe_body_id', label: "项目配方号", width: '200',hidden:true},
            {name: 'recipe_id',      index: 'recipe_id', label: RECIPE_NO_TAG, width: '200'},
            {name: 'body_item',      index: 'body_item', label: "Body Name", width: '200'},
            {name: 'body_dsc',       index: 'body_dsc', label: "Body Name说明", width: '200'},
            {name: 'body_value_min', index: 'body_value_min', label: "Body Value Min", width: '180'},
            {name: 'body_value_max', index: 'body_value_max', label: "Body Value Max", width: '180'},
            {name: 'body_note',      index: 'body_note', label: "备注", width: '180'},
        ];
        domObj.$SetupInfoListGrd.jqGrid({
            url: "",
            datatype: "local",
            mtype: "POST",
            height: 400,
            autowidth: true,
            shrinkToFit: false,
            autoScroll: false,
            rowNum: 20,
            resizable: true,
            rownumbers: true,
            loadonce: true,
            fixed: true,
            viewrecords: true,
            multiselect: true,
            pager: "#SetupInfoListPg",
            colModel: SetupInfoCM
        });
    };
    
    var initSelectFnc = {
        iniToolIdFunc : function(){//初始化设备代码
            var iary = {
            	unit_typ : 'MAIN'
            };
            var inTrxObj = {
                trx_id     : VAL.T_FBPBISTOL ,
                action_flg : 'Q',
                iary : [iary]
            };
            var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if( outTrxObj.rtn_code == _NORMAL ) {
                SelectDom.addSelectArr(domObj.$ToolIDSel, outTrxObj.oary, "tool_id","tool_id",true, true);
            }
        },
        initBodyNameSel: function () {
            var iary = {
                data_cate : 'RECP'
            };
            var inTrxObj = {
                trx_id      : VAL.T_FBPBISDAT ,
                action_flg  : 'Q'        ,
                iary        : [iary]
            };
            var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(  outTrxObj.rtn_code == "0000000" ) {
                SelectDom.addSelectArr_($("#BodyNameSel"), outTrxObj.oary, "data_ext","data_ext","data_desc",true);               	
            }
        }
    };
    
    var btnFunc = {
        query_btn:function(){
        	var ToolIDSel = $.trim(domObj.$ToolIDSel.val());
        	if (!ToolIDSel) {
        		showErrorDialog("", "设备代码不能为空");
        		return false;
        	}
            var iary={
                tool_id_fk: ToolIDSel
            }
        	var RecipeIdTxt = $.trim(domObj.$RecipeIdTxt.val());
            if(RecipeIdTxt){
            	iary.recipe_id = RecipeIdTxt
            }
            var inObj = {
                trx_id : VAL.T_MBPREPMAG,
                action_flg : "Q",
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oaryA, domObj.$SetupInfoListGrd);
            }
        },
        add_btn:function () {
            var ToolIDSel = $.trim(domObj.$ToolIDSel.val());
            var RecipeIdTxt = $.trim(domObj.$RecipeIdTxt.val());
            var BodyNameSel = $.trim(domObj.$BodyNameSel.val());
            var BodyValueMinTxt = $.trim(domObj.$BodyValueMinTxt.val());
            var BodyValueMaxTxt = $.trim(domObj.$BodyValueMaxTxt.val());
            var BodyNoteTxt = $.trim(domObj.$BodyNoteTxt.val());
            if (!ToolIDSel) {
                showErrorDialog("", "设备代码不能为空");
                return false;
            }
            if (!RecipeIdTxt) {
                showErrorDialog("", "Recipe NO不能为空");
                return false;
            }
            if (!BodyNameSel) {
                showErrorDialog("", "body name不能为空");
                return false;
            }
            if (!BodyValueMinTxt) {
                showErrorDialog("", "body value min不能为空");
                return false;
            }
            if (!BodyValueMaxTxt) {
                showErrorDialog("", "body value max不能为空");
                return false;
            }
            var ids = domObj.$SetupInfoListGrd.jqGrid('getDataIDs');
            for(var i=0; i==0 || i<ids.length;i++){
                var rowData = domObj.$SetupInfoListGrd.jqGrid("getRowData",ids[i]);
                if(ToolIDSel == rowData.tool_id_fk && RecipeIdTxt == rowData.recipe_id && BodyNameSel == rowData.body_item){
                    showErrorDialog("", "设备代码[" + ToolIDSel + "]，Recipe NO[" + RecipeIdTxt + "]已设定Body Name[" + rowData.body_item + "]!");
                    return false;
                }
            }
            var iary = {
                tool_id_fk: ToolIDSel,
                recipe_id: RecipeIdTxt,
                body_item: BodyNameSel,
                body_dsc: BodyNameSel,
                body_value_min: BodyValueMinTxt,
                body_value_max: BodyValueMaxTxt,
                body_note: BodyNoteTxt
            };
            var inObj = {
                trx_id: VAL.T_MBPREPMAG,
                evt_usr: VAL.EVT_USER,
                action_flg: 'A',
                iary: [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var newRowId = getGridNewRowID("#SetupInfoListGrd");
                domObj.$SetupInfoListGrd.jqGrid("addRowData", newRowId, iary);
                showSuccessDialog("新增成功");
            }
        },
        delete_btn: function () {
            var ids = domObj.$SetupInfoListGrd.jqGrid('getGridParam', 'selarrrow');
            if (ids.length < 1) {
                showErrorDialog("", "至少选择一条数据进行删除");
                return false;
            }
            var iary = [];
            for (var i = 0; i < ids.length; i++) {
                var rowData = domObj.$SetupInfoListGrd.jqGrid("getRowData", ids[i]);
                var iaryA = {
                    tool_id_fk: rowData.tool_id_fk,
                    recipe_id: rowData.recipe_id,
                    recipe_body_id:rowData.recipe_body_id,
                    body_seq_id : rowData.body_seq_id,
                    body_dsc: rowData.body_dsc,
                    body_value_min: rowData.body_value_min,
                    body_value_max: rowData.body_value_max,
                    body_note: rowData.body_note
                };
                iary.push(iaryA);
            }
            inObj = {
                trx_id: VAL.T_MBPREPMAG,
                evt_usr: VAL.EVT_USER,
                action_flg: 'D',
                iary: iary
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var length = ids.length;
                for (var i = 0; i < length; i++) {
                    domObj.$SetupInfoListGrd.jqGrid("delRowData", ids[0]);
                }
                showSuccessDialog("删除成功");
            }
        }
    };
    
    var initializationFunc = function () {
        iniGridInfo();
    };

    var iniButtonAction =function() {
        domObj.buttons.$delete_btn.click(function () {
            btnFunc.delete_btn();
        });
        domObj.buttons.$query_btn.click(function () {
            btnFunc.query_btn();
        });
        domObj.buttons.$add_btn.click(function () {
            btnFunc.add_btn();
        });
    };
    
    function initFunc() {
        $("input").val("");
        iniButtonAction();
        initializationFunc();
        initSelectFnc.iniToolIdFunc();
        initSelectFnc.initBodyNameSel();
    }
    
    initFunc();
    
    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth;
        divWidth = $("#SetupInfoListDiv").width();
        offsetBottom =  $(window).height() - $("#SetupInfoListDiv").offset().top;
        $("#SetupInfoListDiv").height(offsetBottom * 0.95);
        $("#SetupInfoListGrd").setGridWidth(divWidth * 0.85);
        $("#SetupInfoListGrd").setGridHeight(offsetBottom * 0.99 - 51);

    };
    
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });
});





